One-dimensional array with guard
Make a one-dimensional array of strings with sentry as long as they are newlines. code:python
C = np.zeros((H + 2, W + 2), np.bool_)
C = C.ravel()
H += 2
W += 2
ValueError: could not broadcast input array from shape (3,0) into shape (3,5)
already read all data from stdin
Explanation of each step
code:python
>> np.frombuffer(b"...\n.@.\n...\n", "S1")
array([b'.', b'.', b'.', b'\n', b'.', b'@', b'.', b'\n', b'.', b'.', b'.',
b'\n'], dtype='|S1')
>> _.reshape(3, -1)
>> _ == b"."
>> C = np.zeros((3 + 2, 3 + 2), np.bool_)
>> C
>> C.ravel()
array([False, False, False, False, False, False, True, True, True,
False, False, True, False, True, False, False, True, True,
True, False, False, False, False, False, False])
---
This page is auto-translated from /nishio/番兵付きの一次元配列. If you looks something interesting but the auto-translated English is not good enough to understand it, feel free to let me know at @nishio_en. I'm very happy to spread my thought to non-Japanese readers.